
package demo.model


import net.liftweb._
import util._
import Helpers._
import common._
import json._
import scala.xml.Node
import net.liftweb.mapper._
import net.liftweb.util.FieldError
import net.liftweb.http._
import net.liftweb.common.{Box,Full,Empty,Failure,ParamFailure}
import scala.xml._
import scala.math.BigDecimal
import net.liftweb.json.JsonAST.{JValue,JDouble,JInt}
import net.liftweb.mapper.By



/**
 * define method data Model
 *
 */
class Method extends LongKeyedMapper[Method] with IdPK with OneToMany[Long,Method]{
  def getSingleton = Method

  object name extends MappedString(this,200)

  object description extends MappedTextarea(this,2048)

  object compounds extends MappedOneToMany(Compound,Compound.method, OrderBy(Compound.id,Ascending)) with Cascade[Compound]


}

/**
 * case class of Method for REst api response
 * @param name
 * @param riWindow
 */
case class MethodAsJson(name:String,description:String)
{
  implicit val formats  = DefaultFormats
  def json = Extraction.decompose(this)

}

object Method extends Method with LongKeyedMetaMapper[Method] {

  /**
   * define database table name for dataModel Method
   */
  // override  def dbTableName = "methods"
  /**
   * encoding Method instance into json
   * @param in
   * @return
   */
  def encodeAsJson(in: Method): JsonAST.JObject = encodeAsJSON_!(in)

  // def createFromJson(json: JsonAST.JObject): Method = decodeFromJson_!(json, true)

  /**
   * decoding json into method instance
   * @param json
   * @return
   */
  def buildFromJson(json: JsonAST.JObject, markFieldsAsDirty: Boolean): Method = decodeFromJSON_!(json,markFieldsAsDirty)

  /**
   * update instance using json
   * @param method
   * @param json
   * @return
   */
  def updateFromJSON(method: Method, json: JsonAST.JObject): Method =
    updateFromJSON_!(method, json)

  override def fieldOrder = List(name,description)


  /**
   * get all methods
   */

  private var methods: List[JValue]  = Nil
  val list:List[Method]  = Method.findAll
  list.foreach{ e =>
  //updateListeners(e)
    methods  ::= MethodAsJson(e.name,e.description).json
    //  methods ::=  encodeAsJson(e)
  }



  private var listeners: List[Method => Unit] = Nil



  /**
   *
   * get all method in a sequence
   */

  def  seqMethods : List[JValue] = methods

  /**
   * add New Method
   *
   */
  def add(json: JsonAST.JObject): JsonAST.JObject = {

    val method:Method = buildFromJson(json, true)
    method.validate match{
      case Nil =>{ method.saveMe()
        json  }
      case errors:List[FieldError] => json
    }
  }


  /**
   * search with method name
   *
   */
  def search(str: String): List[JValue] = {
    var searchJson:List[JValue] =Nil
    val strLC = str.toLowerCase()
    var searchList:List[Method]=  Method.findAll(By(Method.name, strLC))
    searchList.foreach{ e =>
    //updateListeners(e)
      searchJson  ::= MethodAsJson(e.name,e.description).json
      //  methods ::=  encodeAsJson(e)
    }
    searchJson
  }


}
